package com.levine.takeoutcostcount;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RvAdapter rvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerview);
        TextView tv_add = findViewById(R.id.tv_add);
        TextView tv_remove = findViewById(R.id.tv_remove);

        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Entity entity = new Entity();
                rvAdapter.entities.add(entity);
                rvAdapter.notifyDataSetChanged();
                recyclerView.smoothScrollToPosition(rvAdapter.entities.size() - 1);
            }
        });
        tv_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        Entity entity = new Entity();
        List<Entity> entities = new ArrayList<>();
        entities.add(entity);
        rvAdapter = new RvAdapter(entities);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.setAdapter(rvAdapter);
    }

    private class RvAdapter extends RecyclerView.Adapter {
        public List<Entity> entities;

        public RvAdapter(List<Entity> entities) {
            this.entities = entities;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            Holder viewHolder = (Holder) holder;
            viewHolder.name.setText(entities.get(position).name);
            viewHolder.price.setText(entities.get(position).price);
            viewHolder.price.addTextChangedListener(new MyTextWatcher(viewHolder.price, position));
            viewHolder.name.addTextChangedListener(new MyTextWatcher(viewHolder.name, position));
        }

        @Override
        public int getItemCount() {
            return entities.size();
        }

        private class Holder extends RecyclerView.ViewHolder {
            public EditText name;
            public EditText price;

            public Holder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                price = itemView.findViewById(R.id.price);
            }
        }
    }

    private class MyTextWatcher implements TextWatcher {
        private EditText editText;
        private int position;

        public MyTextWatcher(EditText editText, int position) {
            this.editText = editText;
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            switch (editText.getId()) {
                case R.id.name:
                    rvAdapter.entities.get(position).name = charSequence.toString();

                    break;
                case R.id.price:
                    rvAdapter.entities.get(position).price = charSequence.toString();
                    break;
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    }

    private class Entity {
        public String name="";
        public String price="";
    }
}
